$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        id:null,
        List:null,
        imgUrl:null,
        fileUrl:null,
        clickFlag:null,
        dom:{
            fileInput:null,
            saveBtn:null
        },
        init:function () {
            this.id = xhTool.getQuery('id');
            this.form      = new xhForm($('#xh_form'));
            this.dom.fileInput = $('#fileInput');
            this.dom.saveBtn   = $('#bt_modal_confirm',parent.document);
            this.funObj().init();//初始化必要的数据
            this.funObj().fileInputEvent();
            this.listener();
        },
        listener:function () {
            var $this = this;
            //保存动作
            $this.dom.saveBtn.click(function () {
                $this.funObj().saveAction();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                init:function () {
                    $this.List      = [];
                    $this.imgUrl    = '';
                    $this.fileUrl   = '';
                    $this.clickFlag = false;

                    fileInput.initFileInput('fileInput',baseUrl+'lm/rent/ajaxUpload');
                    $this.form.bindRequired($('#bt_modal_confirm',parent.document));
                },
                fileInputEvent:function () {
                    var count=0; //记录上传次数
                    $this.dom.fileInput.on("fileuploaded", function (event, data, previewId, index) {
                        count++;
                        $this.List.push({ FileName: data.response.fileName, KeyID: previewId,url:data.response.url })
                        if($this.clickFlag&&(count==data.files.length)){
                            $this.clickFlag=false;
                            count=0;
                            save();
                        }
                    });

                    $this.dom.fileInput.on("filesuccessremove", function (event, data, previewId, index) {
                        for (var i = 0; i < $this.List.length; i++) {
                            if ($this.List[i].KeyID== data) {
                                $this.List.splice(i,1);
                            }
                        }
                    });
                },
                saveAction:function () {
                    if ($('[class=file-preview-thumbnails] [id^=preview] ')[0]!=undefined){//有文件且文件未上传
                        var isUploaded=true;
                        $('[class=file-preview-thumbnails] [id^=preview]').each(function () {
                            if(!$(this).hasClass('file-preview-success')&&!$(this).hasClass('file-preview-error')){
                                isUploaded=false;
                            }
                        });
                        if(isUploaded){//文件已上传
                            save();
                        }else {
                            $this.clickFlag=true;
                            $("#fileInput").fileinput('upload');
                        }
                    }else {
                        save();
                    }
                }
            }
        }
    }
};
function save() {
    obj.form.submit('lm/rent/saveReimburse',{id:obj.id,list:obj.List},function (res) {
       window.top.xhTop.showSuccess(res.msg);
       window.top.xhTop.getActiveFrame()['tabFrame']['rentPayFrame'].obj.dataGrid.reload();
       window.top.xhTop.closeModal();
   });
}